package io.renren.modules.es.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.es.dto.BasePageDto;
import io.renren.modules.es.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/es")
@Api(value = "es测试api", tags = "es测试api")
public class EsController {

    @Autowired
    private ElasticsearchTemplate esTemplate;

    @ApiOperation(value = "es模糊搜索People", notes = "es模糊搜索People", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/queryPeople",method = RequestMethod.POST)
    public R queryPeople(@ModelAttribute BasePageDto basePageDto) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                QueryBuilders.boolQuery()
                        .should(QueryBuilders.matchQuery("name", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("oldName", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("bround", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("idcard", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("workidcard", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("household", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("invitationHousehold", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("invitation", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("invitation2", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("school", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("major", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("spouse", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("address", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("contractYear", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("callPhone", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("bround", basePageDto.getKeyWord()))
        ).withPageable(PageRequest.of(basePageDto.getCurrPage(), basePageDto.getPageSize())).build();
        Page<PeopleESVo> page = esTemplate.queryForPage(searchQuery, PeopleESVo.class);
        return R.ok().put("page", new PageUtils(page.getContent(),(int)page.getTotalElements(),page.getSize(),page.getNumber()));
    }

    @ApiOperation(value = "es模糊搜索PeopleChange", notes = "es模糊搜索PeopleChange", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/queryPeopleChange",method = RequestMethod.POST)
    public R queryPeopleChange(@ModelAttribute BasePageDto basePageDto) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                QueryBuilders.boolQuery()
                        .should(QueryBuilders.matchQuery("name", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("station", basePageDto.getKeyWord()))
                        //.should(QueryBuilders.matchQuery("changeTime", basePageDto.getKeyWord()))
        ).withPageable(PageRequest.of(basePageDto.getCurrPage(), basePageDto.getPageSize())).build();
        Page<PeopleChangeESVo> page = esTemplate.queryForPage(searchQuery, PeopleChangeESVo.class);
        return R.ok().put("page", new PageUtils(page.getContent(),(int)page.getTotalElements(),page.getSize(),page.getNumber()));
    }

    @ApiOperation(value = "es模糊搜索PeopleNotice", notes = "es模糊搜索PeopleNotice", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/queryPeopleNotice",method = RequestMethod.POST)
    public R queryPeopleNotice(@ModelAttribute BasePageDto basePageDto) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                QueryBuilders.boolQuery()
                        .should(QueryBuilders.matchQuery("name", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("chanageBefore", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("chanageAfter", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("chanageAfterClasses", basePageDto.getKeyWord()))
        ).withPageable(PageRequest.of(basePageDto.getCurrPage(), basePageDto.getPageSize())).build();
        Page<PeopleNoticeESVo> page = esTemplate.queryForPage(searchQuery, PeopleNoticeESVo.class);
        return R.ok().put("page", new PageUtils(page.getContent(),(int)page.getTotalElements(),page.getSize(),page.getNumber()));
    }

    @ApiOperation(value = "es模糊搜索PeopleResume", notes = "es模糊搜索PeopleResume", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/queryPeopleResume",method = RequestMethod.POST)
    public R queryPeopleResume(@ModelAttribute BasePageDto basePageDto) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                QueryBuilders.boolQuery()
                        .should(QueryBuilders.matchQuery("name", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("orgazation", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("title", basePageDto.getKeyWord()))
                        //.should(QueryBuilders.matchQuery("startTime", basePageDto.getKeyWord()))
                        //.should(QueryBuilders.matchQuery("endTime", basePageDto.getKeyWord()))
        ).withPageable(PageRequest.of(basePageDto.getCurrPage(), basePageDto.getPageSize())).build();
        Page<PeopleResumeESVo> page = esTemplate.queryForPage(searchQuery, PeopleResumeESVo.class);
        return R.ok().put("page", new PageUtils(page.getContent(),(int)page.getTotalElements(),page.getSize(),page.getNumber()));
    }

    @ApiOperation(value = "es模糊搜索PeopleOut", notes = "es模糊搜索PeopleOut", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/queryPeopleOut",method = RequestMethod.POST)
    public R queryPeopleOut(@ModelAttribute BasePageDto basePageDto) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                QueryBuilders.boolQuery()
                        .should(QueryBuilders.matchQuery("peopleName", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("deptName", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("postName", basePageDto.getKeyWord()))
                        //.should(QueryBuilders.matchQuery("outTime", basePageDto.getKeyWord()))
        ).withPageable(PageRequest.of(basePageDto.getCurrPage(), basePageDto.getPageSize())).build();
        Page<PeopleOutESVo> page = esTemplate.queryForPage(searchQuery, PeopleOutESVo.class);
        return R.ok().put("page", new PageUtils(page.getContent(),(int)page.getTotalElements(),page.getSize(),page.getNumber()));
    }

    @ApiOperation(value = "es模糊搜索Training", notes = "es模糊搜索Training", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/queryTraining",method = RequestMethod.POST)
    public R queryTraining(@ModelAttribute BasePageDto basePageDto) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                QueryBuilders.boolQuery()
                        .should(QueryBuilders.matchQuery("name", basePageDto.getKeyWord()))
                        //.should(QueryBuilders.matchQuery("startTime", basePageDto.getKeyWord()))
                        //.should(QueryBuilders.matchQuery("endTime", basePageDto.getKeyWord()))
        ).withPageable(PageRequest.of(basePageDto.getCurrPage(), basePageDto.getPageSize())).build();
        Page<TrainingESVo> page = esTemplate.queryForPage(searchQuery, TrainingESVo.class);
        return R.ok().put("page", new PageUtils(page.getContent(),(int)page.getTotalElements(),page.getSize(),page.getNumber()));
    }

    @ApiOperation(value = "es模糊搜索TrainingPeople", notes = "es模糊搜索TrainingPeople", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/queryTrainingPeople",method = RequestMethod.POST)
    public R queryTrainingPeople(@ModelAttribute BasePageDto basePageDto) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                QueryBuilders.boolQuery()
                        .should(QueryBuilders.matchQuery("name", basePageDto.getKeyWord()))
        ).withPageable(PageRequest.of(basePageDto.getCurrPage(), basePageDto.getPageSize())).build();
        Page<TrainingPeopleESVo> page = esTemplate.queryForPage(searchQuery, TrainingPeopleESVo.class);
        return R.ok().put("page", new PageUtils(page.getContent(),(int)page.getTotalElements(),page.getSize(),page.getNumber()));
    }

    @ApiOperation(value = "es模糊搜索fightInformation", notes = "es模糊搜索fightInformation", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/queryFightInformation",method = RequestMethod.POST)
    public R queryFightInformation(@ModelAttribute BasePageDto basePageDto) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                QueryBuilders.boolQuery()
                        .should(QueryBuilders.matchQuery("name", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("address", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("stuation", basePageDto.getKeyWord()))
                        //.should(QueryBuilders.matchQuery("time", basePageDto.getKeyWord()))
        ).withPageable(PageRequest.of(basePageDto.getCurrPage(), basePageDto.getPageSize())).build();
        Page<FightInformationESVo> page = esTemplate.queryForPage(searchQuery, FightInformationESVo.class);
        return R.ok().put("page", new PageUtils(page.getContent(),(int)page.getTotalElements(),page.getSize(),page.getNumber()));
    }

    @ApiOperation(value = "es模糊搜索fightPeopleESVo", notes = "es模糊搜索fightPeopleESVo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/queryFightPeople",method = RequestMethod.POST)
    public R queryFightPeople(@ModelAttribute BasePageDto basePageDto) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                QueryBuilders.boolQuery()
                        .should(QueryBuilders.matchQuery("name", basePageDto.getKeyWord()))
                        .should(QueryBuilders.matchQuery("award", basePageDto.getKeyWord()))
        ).withPageable(PageRequest.of(basePageDto.getCurrPage(), basePageDto.getPageSize())).build();
        Page<FightPeopleESVo> page = esTemplate.queryForPage(searchQuery, FightPeopleESVo.class);
        return R.ok().put("page", new PageUtils(page.getContent(),(int)page.getTotalElements(),page.getSize(),page.getNumber()));
    }
}
